package game.players.user.running;

import com.uqbar.vainilla.DeltaState;
import com.uqbar.vainilla.colissions.CollisionDetector;
import com.uqbar.vainilla.events.constants.Key;

import game.Goal3;
import game.ball.Possessed;
import game.ball.SoccerBall;
import game.players.SoccerPlayer;
import game.players.states.WithoutBall;
import game.players.user.movements.WithBallStationaryUser;
import game.players.user.movements.WithoutBallStationaryUser;

public abstract class WithoutBallRunningUserPlayer extends WithoutBall {
	
	private double acceleration = 0;
	
	private static final double MAX_SPEED         = Goal3.MAX_PLAYER_SPEED;
	private static final double MIN_SPEED         = Goal3.PLAYER_SPEED;
	private static final double MAX_ACCELERATION  = 1;
	private static final double ACCELERATION_RATE = 0.005;

	public WithoutBallRunningUserPlayer(SoccerPlayer player) {
		super(player);
	}

	@Override
	public void update(DeltaState deltaState) {
		
		if (!deltaState.isKeyBeingHold(Key.SHIFT)) {
			this.getPlayer().setMovementSpeed(MIN_SPEED);
			this.getPlayer().setState(new WithoutBallStationaryUser(this.getPlayer()));
		}
		
		if (this.getPlayer().getMovementSpeed() < MAX_SPEED && this.getAcceleration() < MAX_ACCELERATION) {
			this.setAcceleration(this.getAcceleration() + ACCELERATION_RATE);
			this.getPlayer().setMovementSpeed(this.getPlayer().getMovementSpeed() + this.getAcceleration());
		}
		else
			this.getPlayer().setMovementSpeed(MAX_SPEED);
		
		super.update(deltaState);
	}
	
	@Override
	public void takePossession(SoccerBall ball) {
		double advanceX = this.getPlayer().getX() + this.getPlayer().getAppearance().getWidth() - ball.getX() - ball.getRatio();
		double advanceY = this.getPlayer().getY() + this.getPlayer().getAppearance().getHeight() - ball.getY() - ball.getRatio();
		
		ball.moveNormally(advanceX, advanceY);
		
		this.getPlayer().setMovementSpeed(MIN_SPEED);
		this.getPlayer().setState(new WithBallStationaryUser(this.getPlayer()));
		ball.setState(new Possessed(this.getPlayer()));
	}

	@Override
	public boolean collidesAgainstBall(SoccerBall ball, SoccerPlayer player) {
		return CollisionDetector.INSTANCE.collidesCircleAgainstRect(
				ball.getX(), ball.getY(), ball.getRatio(),
				player.getX() + player.getAppearance().getWidth() - player.getFeetWidth(), 
				player.getY() + player.getAppearance().getHeight() - player.getFeetHeight(), 
				player.getFeetWidth(),player.getFeetHeight()
				);
	}
	
	
	
	
	
	
	

	public double getAcceleration() {
		return acceleration;
	}

	public void setAcceleration(double acceleration) {
		this.acceleration = acceleration;
	}

}
